package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.LiuhecaiPrizeActivity;

import java.util.List;

/**
 * Created by ASUS on 2017/7/26.
 */

public class PrizeNumberHorizontalAdapter extends RecyclerView.Adapter<PrizeNumberHorizontalAdapter.ViewHolder> implements View.OnClickListener{
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private List<LiuhecaiPrizeActivity.NumberBean> datas;

    public PrizeNumberHorizontalAdapter(final List<LiuhecaiPrizeActivity.NumberBean> datas, final Context context) {
        this.datas = datas;
        this.context = context;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_lhc_prize_item, viewGroup, false);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        viewHolder.number.setText(datas.get(position).getNumber());
        if (datas.get(position).isChoose()) {
            viewHolder.check.setImageResource(R.mipmap.check_ok);
            viewHolder.number.setTextColor(context.getResources().getColor(R.color.purples));
        } else {
            viewHolder.check.setImageResource(R.mipmap.check_none);
            viewHolder.number.setTextColor(context.getResources().getColor(R.color.lottery_black));
        }

        viewHolder.itemView.setTag(position);
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView number;
        public ImageView check;

        public ViewHolder(View view){
            super(view);
            number = view.findViewById(R.id.number);
            check = view.findViewById(R.id.check);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
